package it.bnl.layar;

import java.io.IOException;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.sql.DataSource;


import org.codehaus.jackson.map.ObjectMapper;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;

import it.bnl.layar.Response;
import it.bnl.layar.biz.RequestBiz;
import it.bnl.layar.data.GetParameter;

@SuppressWarnings("serial")
public class BranchLocatorServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(BranchLocatorServlet.class.getName());

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		GetParameter p = new GetParameter();
		resp.setContentType("application/json");
		resp.setCharacterEncoding("UTF-8");
		
		// visualizzazione della richiesta e dell'host da cui � stata inviata.
		log.info("richiesta da : "+ req.getRemoteHost() + " con parametri : " + req.getQueryString());

		//
		
		String lats = (String) req.getParameter("lat");
		String lons = (String) req.getParameter("lon");
		float lat = 0.0f;
		float lon = 0.0f;
		try {
			lat = new Float(lats).floatValue();
			lon = new Float(lons).floatValue();
		} catch (NumberFormatException nfe) {
			System.out.println(nfe);
		}
		String radiuss = (String) req.getParameter("radius");
		int radius = 2000; // raggio entro cui effettuare la ricerca dei POIs.
		if (radiuss != null) {
			radius = new Integer(radiuss).intValue();
		}
		String alts = (String) req.getParameter("alt");
		int alt = 5;
		if (alts != null) {
			alt = new Float(alts).intValue();
		}
		
		p.setLatitudine(lat);
		p.setLongitudine(lon);
		p.setRaggio(radius);
		p.setAltezza(alt);

		log.info("Parametri Ricevuti : "+ p);
		
		// ObjectMapper:
		ObjectMapper mapper = new ObjectMapper();
		Response result = new Response();

		RequestBiz requestBiz = new RequestBiz();
		result = requestBiz.handleRequest(p);
		mapper.writeValue(resp.getWriter(), result);
	}
}